
#include <Unit.h>
#include <FormatOutput.h>
#include <string.h>

Unit::Unit(int factor, const char* name):
    factorForBasicUnit_m(factor),
    name_m(name)
{
}

int Unit::convertToBasicUnit(int amount) const
{  
   return amount*factorForBasicUnit_m;
}

int Unit::getFactorForBasicUnit() const
{
    return factorForBasicUnit_m;
}

const char* Unit::getUnitName() const
{
    return name_m;
}

const void Unit::formatOutput(int amount, std::ostream& stream, const Unit* unit_i, std::list<const Unit*> unitList)
{
    std::list<const Unit*>::const_iterator unit = unitList.begin();
    while (strcmp((*unit)->getUnitName(), unit_i->getUnitName())) unit++;
    for (int remain = amount; remain != 0 && unit != unitList.end(); unit++)
    {
        FormatOutput::getFormatOutput()->output(remain, stream, (*unit)->getFactorForBasicUnit(), (*unit)->getUnitName());
        remain = remain % (*unit)->getFactorForBasicUnit();
    }
}

